const express = require("express");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const { filePath } = require("../config/application.config");
const router = express.Router();

// const upload = multer({ dest: "public/uploads" });\
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, filePath); // 设置文件存储路径
  },
  filename: function (req, file, cb) {
    const uniqueSuffix =
      Date.now() +
      "-" +
      Math.round(Math.random() * 1e9) +
      path.extname(file.originalname);
    cb(null, file.fieldname + "-" + uniqueSuffix); // 设置文件名
  },
});
const upload = multer({ storage });

router.post("/upload", upload.single("file"), (req, res) => {
  res.send({
    code: 0,
    data: `/file_assets/${req.file.filename}`,
    message: "上传成功",
  });
});

module.exports = router;
